<template>
  <div class="platform-detail-home">
    <el-row>
      <el-col :span="13">
        <over-view></over-view>
        <ChartsBox style="margin-top:10px"
                   type="energy"
                   title="能耗趋势"></ChartsBox>
        <ChartsBox style="margin-top:10px"
                   type="electricity"
                   title="电流/电压/功率"></ChartsBox>
        <ChartsBox style="margin-top:10px"
                   type="carbon"
                   title="碳排放"></ChartsBox>
      </el-col>
      <el-col :span="11"
              style="padding-left:15px">
        <map-info :deviceId="deviceId"
                  @selectLocationIdFun="selectLocationIdFun"
                  :allDeviceData="allDeviceData"
                  :platformId="platformId"></map-info>
        <WarningInfo style="margin-top:10px"
                     :alertData="alertData"></WarningInfo>
        <EnergyPie style="margin-top:10px"
                   type="carbon"
                   title="能耗组成"></EnergyPie>
      </el-col>
    </el-row>
  </div>
</template>
<script>
import MapInfo from "./components/mapInfo/index";
import WarningInfo from "./components/warningInfo";
import EnergyPie from "./components/energyPie";
import OverView from "./components/overView";
import ChartsBox from "./components/chartsBox";
import RiskStatistics from "./components/riskStatistics";
import EnvCompliance from "./components/envCompliance/index";
import { getAllDeviceData, getDeviceEvaluate } from "@/api/platformManage";
import store from "@/store/modules/user"
export default {
  name: "warningDevice",
  components: {
    WarningInfo,
    MapInfo,
    OverView,
    RiskStatistics,
    EnvCompliance,
    ChartsBox,
    EnergyPie
  },
  data() {
    return {
      chart: null,
      platformId: "",
      platformName: "",
      platformLogo: "",
      selectDeviceIndex: 0,
      deviceId: "",
      deviceName: "",
      alertData: [],
      allDeviceData: [],
      onlineData: [],
      timer: null,
      evaluateObj: {},
      evaluateList: [//左上角单点评价 GREAT 优，GOOD 良，OK 中，BAD 差
        {
          code: 'GREAT',
          lable: '优'
        },
        {
          code: 'GOOD',
          lable: '良'
        },
        {
          code: 'OK',
          lable: '中'
        },
        {
          code: 'BAD',
          lable: '差'
        },
      ],
    };
  },
  filters: {
    evaluateFil(val) {
      switch (val) {
        case 'GREAT':
          return '优';
        case 'GOOD':
          return '良';
        case 'OK':
          return '中';
        case 'BAD':
          return '差';
      }
    }
  },
  watch: {
    deviceId() {
      this.getDeviceEvaluateFun();
    }
  },
  mounted() {
    // this.$nextTick(() => {
    //   this.initChart();
    // });
    if (this.$route.query.platformId) {
      this.platformId = this.$route.query.platformId;
      this.platformName = this.$route.query.name;
      this.platformLogo = this.$route.query.logo;
      this.$store.commit("user/selectProjectMuts", {
        id: this.platformId,
        name: this.platformName,
        logo: this.platformLogo
      });
    } else {
      var selectProject = JSON.parse(localStorage.getItem('selectProject'));
      this.platformId = selectProject.id;
      this.platformName = selectProject.name;
      this.platformLogo = selectProject.logo;
    }
    this.getAllDeviceFun();
  },
  unmounted() {
    clearInterval(this.timer);
  },
  methods: {
    getDeviceEvaluateFun() {
      getDeviceEvaluate(this.deviceId).then(res => {
        this.evaluateObj = res.value;

      })
    },
    getAllDeviceFun() {
      getAllDeviceData(this.platformId).then(res => {
        let data = res.value;
        this.allDeviceData = data;
        this.alertData = data.filter(e => {
          if (e.data != null) {
            return e.alert == true;
          }
        });
        this.onlineData = data.filter(e => {
          if (e.state == "ONLINE") {
            return e;
          }
        });
        this.selectDeviceIndex = 0;
        this.deviceId = this.onlineData[0].deviceId;
        this.deviceName = this.onlineData[0].name;
      });
      //定时器轮播设备
      this.timer = setInterval(() => {
        if (this.selectDeviceIndex < this.onlineData.length - 2) {
          this.selectDeviceIndex++;
          this.deviceId = this.onlineData[this.selectDeviceIndex].deviceId;
          this.deviceName = this.onlineData[this.selectDeviceIndex].name;
        } else {
          this.selectDeviceIndex = 0;
          this.deviceId = this.onlineData[0].deviceId;
          this.deviceName = this.onlineData[0].name;
        }
      }, 1000 * 60);
    },
    selectLocationIdFun(item) {
      console.log(item);

      this.deviceId = item.id;
      this.deviceName = item.name;
    }
  }
};
</script>
<style>
.platform-detail-home {
  height: 100%;
  margin-top: 16px;
}
body {
  background-color: #edf0f3;
  font-family: Segoe UI;
}
.header {
  height: 59px;
  width: 100%;
  background-color: #2d9dff;
}
</style>
<style lang="scss" scoped>
.main {
  // padding: 15px 15px 0 15px !important;
  box-sizing: content-box;
}
.env-appraise {
  background-color: #fff;
  border-radius: 12px;
  padding: 20px;
  .title {
    font-size: 16px;
    font-weight: bold;
    line-height: 21px;
    color: #0a0a0a;
  }
  .content {
    display: flex;
    justify-content: space-between;
    align-items: center;
    .text {
      font-size: 16px;
      font-weight: 400;
      line-height: 21px;
      color: #535353;
      margin-right: 20px;
    }
  }
}
.evaluate-text {
  background-image: url("../../../assets/platformDetail/great.png");
  min-width: 150px;
  min-height: 100px;
  background-repeat: no-repeat;
  background-size: cover;
  background-position: center;
  text-align: center;
  padding-top: 40px;
  font-weight: bold;
  font-size: 30px;
}
</style>
